package com.ldzy.dao;

import com.ldzy.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public class AccountDaoImpl implements AccountDao {
   private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public int addAccount(Account account) {
        com.ldzy.pojo.Account account1=new com.ldzy.pojo.Account();
        String sql="INSERT  INTO  account(username,balance) VALUE (?,?)";
        Object[] parname=new  Object[]{
                account.getUsername(),
                account.getDouble()
        };

        return  jdbcTemplate.update(sql,parname);
    }

    public int updateAccount(Account account) {
        return 0;
    }

    public int detAccount(int id) {
        return 0;
    }

    public AccountDao findByIdAccount(int id) {
        return null;
    }

    public List<AccountDao> findByAccountAll() {
        return null;
    }
@Transactional
    public Boolean transfer(Account account, Account inUser, Double number) {
        String sql= "UPDATE account set balance=balance=? WHERE id=? ";
        Object [] objects=new Object[]{
            number,
                account.getId()
        };
//        int i=1/0;
      int numb1=  this.jdbcTemplate.update(sql,objects);
         sql= "UPDATE account set balance=balance=? WHERE id=?";
         objects=new Object[]{
                number,
                inUser.getId()
        };
       int nub2= this.jdbcTemplate.update(sql,objects);
       if(numb1>0&&nub2>0){
           return true;
       }
       else
           return false;
    }
}
